var EXPORTED_SYMBOLS = ["dDeviceSqlite"];
var dDeviceSqlite = {
onLoad:function(){
    this.initialized = true;
    this.dbStatus = true;
    this.dbInit();
},
initialized:false,
dbStatus:false,
dbConnection:null,

dbSchema:{
  tables:{
       td_device:"dId           INTEGER PRIMARY KEY AUTOINCREMENT,\
                dName         CHAR(50) NOT NULL,\
                dLocation     TEXT NOT NULL,\
                dUDN          TEXT NOT NULL,\
                dStatus       INTEGER DEFAULT 0,\
                dHandleStatus INTEGER DEFAULT 0,\
                dListId       INTEGER",
      td_account:"aId           INTEGER PRIMARY KEY AUTOINCREMENT,\
                aName         CHAR(50) NOT NULL,\
                aPwd          CHAR(50) NOT NULL,\
                aXml          TEXT NOT NULL,\
                dId           INTEGER NOT NULL,\
                aServiceType  INTEGER,\
                aLocalStatus  INTEGER DEFAULT 0,\
                aRemoteStatus INTEGER DEFAULT 0,\
                aLastStatus   INTEGER DEFAULT 0,\
                aControlType  INTEGER DEFAULT 0",                 
      td_email:"eId           INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
                eMessageId   INTEGER NOT NULL,\
                eSubject      VARCHAR NOT NULL,\
                eFrom      VARCHAR NOT NULL,\
                eTo      VARCHAR NOT NULL,\
                eReply      VARCHAR NULL,\
                eFolder     VARCHAR NOT NULL,\
                eName     VARCHAR NOT NULL,\
                eDate      DATETIME NOT NULL,\
                dId   INTEGER NOT NULL",
         td_contact:"cId           INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
                cDeviceName        VARCHAR NOT NULL,\
                cContactName         VARCHAR NOT NULL,\
                cEmailAddress      VARCHAR NOT NULL,\
                cAccount               VARCHAR NOT NULL,\
                cUri                       VARCHAR,\
                cStatus                  INTEGER DEFAULT 1"
  }
},

dbInit:function(){
  
  var dirService = Components.classes["@mozilla.org/file/directory_service;1"].
          getService(Components.interfaces.nsIProperties);
  
  var dbFile = dirService.get("ProfD", Components.interfaces.nsIFile);
  dbFile.append("deviceinfo.sqlite");
  
  var dbService = Components.classes["@mozilla.org/storage/service;1"].
          getService(Components.interfaces.mozIStorageService);
  
  var dbConnection;
  
  if(!dbFile.exists())
      dbConnection = this._dbCreate(dbService,dbFile);
  else{
      dbConnection = dbService.openDatabase(dbFile);
  }
  this.dbConnection = dbConnection;
},

_dbCreate:function(aDBService, aDBFile){
  var dbConnection = aDBService.openDatabase(aDBFile);
  this._dbCreateTables(dbConnection);
  return dbConnection;
},

_dbCreateTables:function(aDBConnection){
  for(var name in this.dbSchema.tables){
      aDBConnection.createTable(name, this.dbSchema.tables[name]);
  }
},
onClose:function(){
    this.initialized = false;
    this.dbStatus=false;
    this.dbConnection.asyncClose();
}
};